<?php
	require("modules/security_mod.php");
	require("modules/mission_mod.php");
	require("npc_images.php");
	require("page_navigator.php");
	SecurityMod::login();

	$pageNumber = intval(v($_REQUEST, "page"));
	if ($pageNumber < 1)
		$pageNumber = 1;
	$filters["universe"] = $_SESSION["account"]->getUniverse();
	$filters["type"] = v($_REQUEST, "type");
	$filters["daterange"] = v($_REQUEST, "daterange");
	$filters["dateto"] = v($_REQUEST, "dateto");
	$filters["npc"] = v($_REQUEST, "npc");
	$filters["faction"] = v($_REQUEST, "faction");
	$missions = MissionMod::getMissions($filters, $pageNumber, $pageCount);

	$missionTypes =
		array(
			"Assassination",
			"Transport Packages",
			"Transport Explosives",
			"Transport VIP",
			"VIP Action Trip",
			"Cleaning Wormhole Exit",
			"Scout",
			"Transport Spy",
			"Espionage",
			"Transport Military Explosives"
		);
	$dateRanges = array("Today", "Yesterday");
	$npcs = split(".png|.gif", trim($npcImages));
	$factions = array("emp" => "Empire", "fed" => "Federation", "uni" => "Union", "eps" => "EPS", "tss" => "TSS", "neu" => "Neutral");
	array_pop($npcs);

	function drawNavigator() {
		global $pageCount, $pageNumber, $filters;
		$params = "";
		foreach ($filters as $name => $filter) {
			if ($filter)
				$params .= sprintf("&%s=%s", $name, $filter);
		}
		PageNavigator::draw($pageCount, $pageNumber, 29, $params, "missions.php");
	}
?>
<html>
<head>
<link rel="stylesheet" href="main.css">
<script src="main.js" type="text/javascript"></script>
<script language="javascript">
	function onSubmit() {
		var daterange = document.getElementById("daterange");
		var dateto = document.getElementById("dateto");
		if (daterange.value != "") {
			var date = new Date();
			date.setHours(0, 0, 0, 0);
			dateto.value = date.getTime();
		}
		return true;
	}
</script>
</head>
<body>
	<h2 align="center">Missions</h2>
	<table align="center" width="800">
	<tr>
		<td>
			<form action="missions.php" method="GET" onsubmit="onSubmit()" style="margin-bottom:0;">
				<input type="hidden" name="universe" value="<?php echo($filters["universe"])?>"/>
				<input type="hidden" name="dateto" id="dateto"/>
				<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align='center'>
				<tr>
					<td>
						<table>
						<tr>
							<td>
								<label>Type:&nbsp;</label>
								<select name="type" style="width:120">
									<option value="">All</option>
									<?php foreach ($missionTypes as $missionType):?>
									<option value="<?php echo($missionType)?>" <?php if ($missionType == $filters["type"]) echo("selected")?>><?php echo($missionType)?></option>
									<?php endforeach?>
								</select>
							</td>
							<td width="10">&nbsp;</td>
							<td>
								<label>Date:&nbsp;</label>
								<select name="daterange" id="daterange" style="width:120">
									<option value="">All</option>
									<?php foreach ($dateRanges as $dateRange):?>
									<option value="<?php echo($dateRange)?>" <?php if ($dateRange == $filters["daterange"]) echo("selected")?>><?php echo($dateRange)?></option>
									<?php endforeach?>
								</select>
							</td>
							<td width="10">&nbsp;</td>
							<td>
								<label>Faction:&nbsp;</label>
								<select name="faction" style="width:120">
									<option value="">All</option>
									<?php foreach ($factions as $code => $faction):?>
									<option value="<?php echo($code)?>" <?php if ($code == $filters["faction"]) echo("selected")?>><?php echo($faction)?></option>
									<?php endforeach?>
								</select>
							</td>
							<td width="10">&nbsp;</td>
							<td>
								<label>Opponent:&nbsp;</label>
								<select name="npc" style="width:120">
									<option value="">All</option>
									<?php foreach ($npcs as $npc):?>
									<option value="<?php echo($npc)?>" <?php if ($npc == $filters["npc"]) echo("selected")?>><?php echo($npc)?></option>
									<?php endforeach?>
								</select>
							</td>
						</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td align="center"><input type="submit" value="Filter"></td>
				</tr>
				</table>
			</form>
		</td>
	</tr>
	<tr>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td align="center">
			<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center" width="100%">
			<tr>
				<td colspan="10"><?php drawNavigator()?></td>
			</tr>
			<tr>
				<th>&nbsp;</th>
				<th><u>Date</u></th>
				<th><u>Universe</u></th>
				<th><u>Source</u></th>
				<th><u>Faction</u></th>
				<th><u>Rank</u></th>
				<th><u>Destination</u></th>
				<th><u>Time limit</u></th>
				<th><u>Target</u></th>
				<th><u>Type</u></th>
				<th><u>Reward</u></th>
			</tr>
			<?php
				$i = 0;
				$missionrank = "";
				$targetrank = "";
				foreach ($missions as $mission):
					$i++;
					$imgSrc = "";
					$imgWidth = 32;
					if ($mission["type"] == "Assassination")
						$imgSrc = "/opponents/" . $mission["opponent"] . ".png";
					else
					if ($mission["type"] == "VIP Action Trip" || $mission["type"] == "Transport VIP") {
						$imgSrc = "/vip.png";
						$imgWidth = 13;
					} else
					if ($mission["type"] == "Transport Packages")
						$imgSrc = "/packages.png";
					else
					if ($mission["type"] == "Transport Explosives")
						$imgSrc = "/explosives.png";
					else
					if ($mission["type"] == "Cleaning Wormhole Exit")
						$imgSrc = "/foregrounds/wormhole.gif";
					else
					if ($mission["type"] == "Scout")
						$imgSrc = "/scout.png";
					else
					if ($mission["type"] == "Transport Spy"){
						$imgSrc = "/vip.png";
						$imgWidth = 13;
					}
					else
					if ($mission["type"] == "Espionage"){
						$imgSrc = "/espionage.png";
						switch ($mission["opponent"]) {
							case "emprank1":
								$targetrank = "Outsider";
								break;
							case "emprank2":
								$targetrank = "Serf";
								break;
							case "emprank3":
								$targetrank = "Master / Mistress";
								break;
							case "emprank4":
								$targetrank = "Sir / Dame";
								break;
							case "emprank5":
								$targetrank = "Squire";
								break;
							case "emprank6":
								$targetrank = "Lord / Lady";
								break;
							case "emprank7":
								$targetrank = "Baron / Baroness";
								break;
							case "emprank8":
								$targetrank = "Viscount / Viscountess";
								break;
							case "emprank9":
								$targetrank = "Count / Countess";
								break;
							case "emprank10":
								$targetrank = "Earl / Marchesa";
								break;
							case "emprank11":
								$targetrank = "Marquis / Marchioness";
								break;
							case "emprank12":
								$targetrank = "Duke / Duchess";
								break;
							case "emprank13":
								$targetrank = "Prince / Princess";
								break;
							case "emprank14":
								$targetrank = "King / Queen";
								break;
							case "unirank1":
								$targetrank = "Flunkey";
								break;
							case "unirank2":
								$targetrank = "Minion";
								break;
							case "unirank3":
								$targetrank = "Acolyte";
								break;
							case "unirank4":
								$targetrank = "Bookie";
								break;
							case "unirank5":
								$targetrank = "Collector";
								break;
							case "unirank6":
								$targetrank = "Accountant";
								break;
							case "unirank7":
								$targetrank = "Cleaner";
								break;
							case "unirank8":
								$targetrank = "Made Man / Made Woman";
								break;
							case "unirank9":
								$targetrank = "Chief";
								break;
							case "unirank10":
								$targetrank = "Kingpin";
								break;
							case "unirank11":
								$targetrank = "Nephew / Niece";
								break;
							case "unirank12":
								$targetrank = "Son / Daughter";
								break;
							case "unirank13":
								$targetrank = "Godfather / Godmother";
								break;
							case "unirank14":
								$targetrank = "Mogul";
								break;
							default:
								$targetrank = "";
						}
					} else
					if ($mission["type"] == "Transport Military Explosives")
						$imgSrc = "/explosives_military.png";
						
					switch ($mission["rank"]) {
						case "fedrank1":
							$missionrank = "Recruit";
							break;
						case "fedrank2":
							$missionrank = "Private";
							break;
						case "fedrank3":
							$missionrank = "Corporal";
							break;
						case "fedrank4":
							$missionrank = "Sergeant";
							break;
						case "fedrank5":
							$missionrank = "Sgt-Major";
							break;
						case "fedrank6":
							$missionrank = "Major";
							break;
						case "fedrank7":
							$missionrank = "Colonel";
							break;
						case "fedrank8":
							$missionrank = "Lieutenant";
							break;
						case "fedrank9":
							$missionrank = "Lt. Commander";
							break;
						case "fedrank10":
							$missionrank = "Captain";
							break;
						case "fedrank11":
							$missionrank = "Commodore";
							break;
						case "fedrank12":
							$missionrank = "Rear Admiral";
							break;
						case "fedrank13":
							$missionrank = "Admiral";
							break;
						case "fedrank14":
							$missionrank = "Fleet Admiral";
							break;
						default:
							$missionrank = "";
					}

			?>
			<tr bgcolor="#0b0b2f">
				<td align="right">
					<?php echo(($pageNumber - 1) * SettingsMod::RECORDS_PER_PAGE + $i)?>.
				</td>
				<!--<td>
					<script language="javascript">document.write(formatDate(<?php echo(strtotime($mission["when"]) * 1000)?>))</script>
				</td>-->
				<td align="center"><?php echo($mission["when"])?></td>
				<td align="center"><?php echo($mission["universe"])?></td>
				<td align="center"><?php echo($mission["source"])?></td>
                <td align="center"><?php echo($mission["faction"] ? "<img src=\"" . SettingsMod::STATIC_IMAGES . "/factions/sign_" . $mission["faction"] . "_16x16.png\"/>" : "&nbsp;")?></td>
                <td align="center"><?php echo($mission["faction"] ? "<img src=\"" . SettingsMod::STATIC_IMAGES . "/factions/" . $mission["rank"] . ".png\" title=\"" . $missionrank . "\"/>" : "&nbsp;")?></td>
				<td align="center">
					<?php if ($mission["sector"]) echo($mission["sector"] . " [" . $mission["coords"] . "]");?>
					<?php if ($mission["destination"]):?>
					<br><font size="1"><?php echo($mission["destination"])?></font>
					<?php endif;?>
				</td>
				<td align="center"><?php echo($mission["timelimit"])?></td>
				<td align="center"><?php echo($mission["opponent"] ? "<img src=\"" . SettingsMod::STATIC_IMAGES . "/factions/" . $mission["opponent"] . ".png\" title=\"" . $targetrank . "\"/>" : "&nbsp;")?></td>
				<td align="center"><?php echo($imgSrc ? "<img src=\"" . SettingsMod::STATIC_IMAGES . $imgSrc . "\" width=\"" . $imgWidth . "\" height=\"32\" title=\"" . $mission["type"] . "\"/>" : "&nbsp;")?></td>
				<td align="center"><?php echo($mission["reward"])?>&nbsp;<img src="<?php echo(SettingsMod::STATIC_IMAGES)?>/credits.png"/></td>
			</tr>
			<?php endforeach?>
			<tr>
				<td colspan="10"><?php drawNavigator()?></td>
			</tr>
			</table>
		</td>
	</tr>
	</table>
</body>
</html>